package dao.impl;

import bean.Detail;
import dao.DetailDao;
import util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class DetailDaoImpl implements DetailDao {

    @Override
    public int insert(Detail detail) {
        Connection conn = null;
        PreparedStatement ps = null;
        int num = 0;

        try {
            conn = JDBCUtil.getConn();
            String sql = "insert into DETAIL(dno,fromNo,operate,toNo,toUser,money,ddate) values (seq_de_dno.nextval,?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);

            //ps.setInt(1, detail.getDno());
            ps.setString(1, detail.getFromNo());
            ps.setString(2, detail.getOperate());
            ps.setString(3, detail.getToNo());
            ps.setString(4, detail.getToUser());
            ps.setFloat(5, detail.getMoney());
            ps.setString(6, detail.getDate());

            num = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(ps, conn);
        }
        return num;
    }

    @Override
    public ArrayList<Detail> selectByFromNo(String fromNo) {
        ArrayList<Detail> detailList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = JDBCUtil.getConn();
            String sql = "select * from DETAIL where FROMNO = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, fromNo);

            rs = ps.executeQuery();
            while (rs.next()) {
                Detail detail = new Detail();
                detail.setDno(rs.getInt("dno"));
                detail.setFromNo(rs.getString("fromNo"));
                detail.setOperate(rs.getString("operate"));
                detail.setToNo(rs.getString("toNo"));
                detail.setToUser(rs.getString("toUser"));
                detail.setMoney(rs.getFloat("money"));
                detail.setDate(rs.getString("ddate"));
                detailList.add(detail);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, ps, conn);
        }
        return detailList;
    }
}
